package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;

import com.sky.vo.DishCategoryVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping
    @ApiOperation(value = "新增菜品接口")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("菜品的相关信息为,{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);
        String key = "dish_"+dishDTO.getCategoryId(); //精确清理
        cleanRedisCache(key);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<PageResult<DishVO>> pageQuery(DishPageQueryDTO dishPageQueryDTO){
            log.info("菜品分页查询{}",dishPageQueryDTO);
            PageResult<DishVO> pageResult =  dishService.pageQuery(dishPageQueryDTO);
            return Result.success(pageResult);

    }
    @DeleteMapping
    @ApiOperation(value = "批量删除菜品")
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除的菜品id为{}",ids);
        dishService.deleteBarch(ids);
        //清理缓存
        cleanRedisCache("dish_*"); //删除所有的dish_id
        return Result.success();
    }
    @ApiOperation(value = "根据菜品id来查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id){
            log.info("查询的菜品id为{}",id);
           DishVO dishVO =  dishService.getDishById(id);
           return Result.success(dishVO);
    }
    @ApiOperation(value = "修改菜品")
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("需要修改的菜品为，{}",dishDTO);
        //清理缓存
        dishService.updateDish(dishDTO);
        cleanRedisCache("dish_*"); //删除所有的dish_id
        return Result.success();

    }
    @ApiOperation(value = "商品起售，停售")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,Long id){
        log.info("商品起售，停售状态{}，id{}",status,id);
        dishService.updateStatus(status,id);
        //清理缓存
        cleanRedisCache("dish_*"); //删除所有的dish_id
        return Result.success();
    }
    @ApiOperation(value = "根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<DishCategoryVO>> getByCategoryId(@RequestParam("categoryId") Long categoryId){
        log.info("分类id为",categoryId);
        List<DishCategoryVO> dishCategoryVOList = dishService.getByCategoryId(categoryId);
             return Result.success(dishCategoryVOList);
    }
    //清空缓存
    private void cleanRedisCache(String pattern){
        Set keys = redisTemplate.keys(pattern);//获取key 通配符
        redisTemplate.delete(keys); //删除key

    }

}
